<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/common/head.jsp"%>
</head>
<body class="${cookie.bodyClass.value}">
	<div class="container body">
		<div class="main_container">
			<%@ include file="/WEB-INF/common/left.jsp"%>
			<%@ include file="/WEB-INF/common/top.jsp"%>
			<!-- page content -->
			<div class="right_col" role="main" id="main">
				<div class="x_panel">
					<div class="x_title">
						<h2>一级商品分类列表</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<form role="form" id="searchForm">
							<div class="container">
								<div class="row">
									<div class="col-lg-3">
										<input class="form-control input-sm" placeholder="标题" name="map['categoryName']"/>
									</div>
									<div class="col-lg-3">
										<input type="hidden" name="map['isDelete']" value="0">
										<input type="hidden" name="map['level']" value="0">
										<button type="button" class="btn btn-success btn-sm" onclick="add();">新增</button>
										<button type="button" class="btn btn-primary btn-sm" onclick="search();">搜索</button>
									</div>
								</div>
							</div>
						</form>
						<div class="table-responsive">
							<table id="banners" class="table table-bordered table-striped table-hover" 
							  cellspacing="0" width="100%">
								<thead>
									<tr class="mytableHead">
										<th class="myRowNumber"></th>
									<!-- 	<th>id</th> -->
										<th>图片</th>
										<th>标题</th>
										<th>商品分类等级</th>
										<th>时间</th>
										<th>操作</th>
									</tr>
								</thead>
							</table>
						</div>
					</div>
				</div>
			</div>
			<!-- /page content -->
			<%@ include file="/WEB-INF/common/foot.jsp"%>
		</div>
	</div>
	<%@ include file="/WEB-INF/common/script.jsp"%>
	<script>
	$(function(){
		var t = $("#banners").DataTable({
		    ajax: {
		        url: ctx+'admin/catagory/findCatagorys',
		        type: 'POST',
		        data: function(d){$.extend(d,serializeObject($("#searchForm")))}
		    },
		    columns: [
		        { "data":null,"render":rowNumberRender},
		      /*   { "data":'id'}, */
		        { "data":'imageUrl',"render":imageUrlRender},
		        { "data":'categoryName'},
		        { "data":'level',"render":rowLevelName},
		        { "data":'createDate'},
		        { "data":'id',"render":optRender}
		    ]
		    
		});
		$("#searchForm input").keydown(function(event){
			if(event.keyCode == 13){
				search();
			}
		});
		$("#searchForm select").change(search);
	});
	
	function optRender(data, type, row, meta){
		var but = '<a class="btn btn-info btn-xs" href="javascript:view('+meta.row+')"><i class="fa fa-pencil"></i>编辑</a>';
		but = but + '<a class="btn btn-danger btn-xs" href="javascript:deleteById('+row.id+')"><i class="fa fa-remove"></i>删除</a>';
		return but;
	}
	function add(){
		window.location = ctx+"admin/catagory/toCatagoryDetailPage";
	} 
	function view(index,event){
		var row = $('#banners').DataTable().rows().data()[index];
		window.location = ctx+"admin/catagory/toCatagoryDetailPage?catagoryId="+row.id;
	}
	
	function rowNumberRender(data, type, row, meta) {
	    var setting = meta.settings;
	    var rowNumber = setting._iDisplayStart + (meta.row + 1);
		return rowNumber;
  	}
	
	function imageUrlRender(data, type, row, meta){
		var imageUrl=row.imageUrl;
		return "<img style='width:50px;height:50px' src='"+imgDomain+"/"+imageUrl+"'/>";
	}

	function rowLevelName(data, type, row, meta) {
	    var levelName =row.level;
	    if(Number(levelName)==0){
            levelName = '一级分类';
		}else if(Number(levelName)==1){
            levelName = '二级分类';
		}
		return levelName;
	}
	function search(){
		$('#banners').DataTable().ajax.reload(function(d){$.extend(d,serializeObject($("#searchForm")))}, true);
	}
	function deleteById(id){
		swal({ "title": "", "text": "确定要删除该一级分类" ,"type": "warning","showCancelButton":true},function(isConfirm){
			if(isConfirm){
				$.ajax({
					url: ctx+"admin/catagory/deleteCatagory?catagoryId="+id+"&time="+(new Date()).getTime(),
					dataType: "json",
					success: function(result){
						if(result.status == "0"){
							search();
						}else{
							swal({title:'',text:result.msg,type:"error"});
						}
					}
				});
			}
		});
	}
	</script>
</body>
</html>